package com.webproject.action.car;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import com.webproject.core.BaseAction;
import com.webproject.core.Page;
import com.webproject.entity.carManager.CarPosition;
import com.webproject.entity.carManager.StopCar;
import com.webproject.entity.carManager.StopCarCost;
import com.webproject.entity.emplayeeManager.Employee;
import com.webproject.service.carManager.CarManagerService;

public class CarManagerAction extends BaseAction{
	private Page mpage;
	private CarManagerService carManagerService;
	private List<CarPosition> carPositions;
	private List<StopCarCost> carCosts;
	public List<StopCarCost> getCarCosts() {
		return carCosts;
	}
	public void setCarCosts(List<StopCarCost> carCosts) {
		this.carCosts = carCosts;
	}
	public Page getMpage() {
		return mpage;
	}
	public void setMpage(Page mpage) {
		this.mpage = mpage;
	}
	public List<CarPosition> getCarPositions() {
		return carPositions;
	}
	public void setCarPositions(List<CarPosition> carPositions) {
		this.carPositions = carPositions;
	}
	public void setCarManagerService(CarManagerService carManagerService) {
		this.carManagerService = carManagerService;
	}
	public String queryCarPosition(){
		mpage = getPage();
		carManagerService.queryCarPosition(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	public String queryCarPositionAll(){
		mpage = getPage();
		mpage.setRow(10000);
		carManagerService.queryCarPosition(mpage);
		carPositions = (List<CarPosition>) mpage.getRows();
		return SUCCESS;
	}
	
	public String queryStopCarAll(){
		mpage=getPage();
		mpage.setRow(10000);
		carManagerService.queryStopCar(mpage);
		carCosts=(List<StopCarCost>) mpage.getRows();
		return SUCCESS;
	}
	public String editCarPosition(){
		CarPosition carPosition=getCarPosition();
		if (getOper().equals("add")) {
			carManagerService.saveCarPosition(carPosition);
		} else if (getOper().equals("edit")) {
			carManagerService.updateCarPosition(carPosition);
		} else if (getOper().equals("del")) {
			carManagerService.deleteCarPosition(carPosition);
		}
		return SUCCESS;
	}
	private CarPosition getCarPosition(){
		String id=getRequest().getParameter("id");
		String position=getRequest().getParameter("position");
		String area=getRequest().getParameter("area");
		String remark=getRequest().getParameter("remark");
		CarPosition carPosition=new CarPosition();
		carPosition.setId(id);
		carPosition.setArea(area);
		carPosition.setPosition(position);
		carPosition.setRemark(remark);
		return carPosition;
	}
	public String queryStopCar(){
		mpage = getPage();
		carManagerService.queryStopCar(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	public String editStopCar(){
		StopCar stopCar=getStopCar();
		if (getOper().equals("add")) {
			carManagerService.saveStopCar(stopCar);
		} else if (getOper().equals("edit")) {
			carManagerService.updateStopCar(stopCar);
		} else if (getOper().equals("del")) {
			carManagerService.deleteStopCar(stopCar);
		}
		return SUCCESS;
	}
	private StopCar getStopCar(){
		StopCar stopCar=new StopCar();
		String id=getRequest().getParameter("id");
		String positionId=getRequest().getParameter("positionId");
		String name=getRequest().getParameter("name");
		String houseCode=getRequest().getParameter("houseCode");
		String regesterTime=getRequest().getParameter("regesterTime");
		String type=getRequest().getParameter("type");
		stopCar.setId(id);
		stopCar.setPositionId(positionId);
		stopCar.setName(name);
		stopCar.setHouseCode(houseCode);
		stopCar.setType(type);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(regesterTime !=null && !regesterTime.isEmpty()){
			try {
				stopCar.setRegesterTime(sdf.parse(regesterTime));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return stopCar;
	}
	public String queryStopCarCost(){
		mpage = getPage();
		carManagerService.queryStopCarCost(mpage);
		setTotal(mpage);
		return SUCCESS;
	}
	public String editStopCarCost(){
		StopCarCost cost=getStopCarCost();
		if (getOper().equals("add")) {
			carManagerService.saveStopCarCost(cost);
		} else if (getOper().equals("edit")) {
			carManagerService.updateStopCarCost(cost);
		} else if (getOper().equals("del")) {
			carManagerService.deleteStopCarCost(cost);
		}
		return SUCCESS;
	}
	private StopCarCost getStopCarCost(){
		StopCarCost scc=new StopCarCost();
		String id=getRequest().getParameter("id");
		String stopCarId=getRequest().getParameter("stopCarId");
		String name=getRequest().getParameter("name");
		String time=getRequest().getParameter("time");
		String money=getRequest().getParameter("money");
		scc.setId(id);
		scc.setStopCarId(stopCarId);
		scc.setName(name);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			if(isNotEmpty(time)){
				scc.setTime(sdf.parse(time));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(isNotEmpty(money)){
			scc.setMoney(new Float(money));
		}
		
		return scc;
	}
}
